package com.dao.impl;

import com.bean.Express;
import com.bean.User;
import com.dao.BaseUserDao;
import com.exception.DuplicateCodeException;
import com.exception.DuplicateUserPhoneException;
import com.util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoMysql extends DBUtils implements BaseUserDao {
    private static final String SQL_CONSOLE = "SELECT " +
            "COUNT(id) data1_size," +
            "COUNT(TO_DAYS(regtime)=TO_DAYS(NOW()) OR NULL) data1_day " +
            "FROM user";
    private static final String SQL_FIND_LIMIT = "SELECT * FROM user LIMIT ?,?";
    private static final String SQL_FIND_ALL = "SELECT * FROM user";
    private static final String SQL_FIND_BY_USER_PHONE = "SELECT * FROM user WHERE userphone=?";
    private static final String SQL_INSERT = "INSERT INTO user (username,userphone,cardnumber,password,regtime,lastlogintime) " +
            "VALUES(?,?,?,?,NOW(),NULL)";
    private static final String SQL_UPDATE = "UPDATE user SET username=?,userphone=?,cardnumber=?,password=? WHERE id=?";
    private static final String SQL_DELETE = "DELETE FROM user WHERE id=?";
    private static final String SQL_UPDATE_LAST_LOGIN_TIME = "UPDATE user SET lastlogintime=NOW() WHERE userphone=?";
    private static final String SQL_INSERT_BY_USER_PHONE = "INSERT INTO user (userphone,password,regtime,lastlogintime) " +
            "VALUES(?,000000,NOW(),NULL)";
    @Override
    public List<Map<String, Integer>> console() {
        ArrayList<Map<String, Integer>> data = new ArrayList<>();
        resultSet = query(SQL_CONSOLE, null);
        try {
            if (resultSet.next()) {
                int data1_size = resultSet.getInt("data1_size");
                int data1_day = resultSet.getInt("data1_day");
                Map data1 = new HashMap();
                data1.put("data1_size", data1_size);
                data1.put("data1_day", data1_day);
                data.add(data1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return data;
    }

    @Override
    public List<User> findAll(boolean limit, int offset, int pageNumber) {
        ArrayList<User> data = new ArrayList<>();
        if (limit) {
            ArrayList arrayList = new ArrayList();
            arrayList.add(offset);
            arrayList.add(pageNumber);
            resultSet = query(SQL_FIND_LIMIT, arrayList);
        } else {
            resultSet = query(SQL_FIND_ALL, null);
        }
        try {
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUserName(resultSet.getString("username"));
                user.setUserPhone(resultSet.getString("userphone"));
                user.setCardNumber(resultSet.getString("cardnumber"));
                user.setPassword(resultSet.getString("password"));
                user.setRegTime(resultSet.getTimestamp("regtime"));
                user.setLastLoginTime(resultSet.getTimestamp("lastlogintime"));
                data.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return data;
    }

    @Override
    public User findByPhone(String userPhone) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(userPhone);
        resultSet = query(SQL_FIND_BY_USER_PHONE, arrayList);
        User user = new User();
        try {
            if (resultSet.next()) {
                user.setId(resultSet.getInt("id"));
                user.setUserName(resultSet.getString("username"));
                user.setUserPhone(resultSet.getString("userphone"));
                user.setCardNumber(resultSet.getString("cardnumber"));
                user.setPassword(resultSet.getString("password"));
                user.setRegTime(resultSet.getTimestamp("regtime"));
                user.setLastLoginTime(resultSet.getTimestamp("lastlogintime"));
            } else {
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public boolean insert(User user) throws DuplicateUserPhoneException {
        ArrayList arrayList = new ArrayList();
        arrayList.add(user.getUserName());
        arrayList.add(user.getUserPhone());
        arrayList.add(user.getCardNumber());
        arrayList.add(user.getPassword());
        int i = 0;
        try {
            i = update(SQL_INSERT, arrayList);
        } catch (Exception e) {
            if (e.getMessage().endsWith("for key 'user.userphone'")) {
                DuplicateUserPhoneException duplicateUserPhoneException = new DuplicateUserPhoneException(e.getMessage());
                throw duplicateUserPhoneException;
            } else {
                e.printStackTrace();
            }
        } finally {
            closeAll();
        }
        if (i > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean insert(String userPhone) throws DuplicateUserPhoneException {
        ArrayList arrayList = new ArrayList();
        arrayList.add(userPhone);
        int i = 0;
        try {
            i = update(SQL_INSERT_BY_USER_PHONE, arrayList);
        } catch (Exception e) {
            if (e.getMessage().endsWith("for key 'user.userphone'")) {
                DuplicateUserPhoneException duplicateUserPhoneException = new DuplicateUserPhoneException(e.getMessage());
                throw duplicateUserPhoneException;
            } else {
                e.printStackTrace();
            }
        } finally {
            closeAll();
        }
        if (i > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean update(int id, User user) throws DuplicateUserPhoneException {
        ArrayList arrayList = new ArrayList();
        arrayList.add(user.getUserName());
        arrayList.add(user.getUserPhone());
        arrayList.add(user.getCardNumber());
        arrayList.add(user.getPassword());
        arrayList.add(id);
        int i = 0;
        try {
            i = update(SQL_UPDATE, arrayList);
        } catch (Exception throwables) {
            if (throwables.getMessage().endsWith("for key 'user.userphone'")) {
                DuplicateUserPhoneException duplicateUserPhoneException = new DuplicateUserPhoneException(throwables.getMessage());
                throw duplicateUserPhoneException;
            } else {
                throwables.printStackTrace();
            }
        } finally {
            closeAll();
        }
        if (i > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean updateLastLoginTime(String userPhone) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(userPhone);
        int i = 0;
        try {
            i = update(SQL_UPDATE_LAST_LOGIN_TIME, arrayList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(int id) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(id);
        int i = 0;
        try {
            i = update(SQL_DELETE, arrayList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (i > 0)
            return true;
        else
            return false;
    }
}
